<link rel="stylesheet" type="text/css" href="src/css/view/commonAdd.css">
<div class="layui-col-md12">
  <div class="layui-card">
    <div class="layui-card-header">类型添加</div>
    <div class="layui-card-body">
      <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
          <label class="layui-form-label">系统</label>
          <div class="layui-input-inline">
            <select name="system" lay-verify="required" lay-filter="system">
            </select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">类型名称</label>
          <div class="layui-input-block">
            <input name="name" autocomplete="off" placeholder="设备类型名称" class="layui-input" type="text" lay-verify="required" style="width:329px;">
          </div>
        </div>
        
        <div class="layui-form-item">
          <button class="layui-btn" lay-submit="" lay-filter="add">添加</button>
          <button class="layui-btn layui-btn-primary" lay-filter="cancel">取消</button>
        </div>
      </form>
    </div>
  </div>
</div>



<script>
var access_token = sessionStorage.access_token;
var userId = sessionStorage.userid;

layui.use(['form','laydate'], function(){
  var form = layui.form;
  form.render();
  
  var selectData;
  //获取select选项
  $.ajax({
    url: 'actionApi/Type/select',
    type:'post',
    data:{"userId":userId},
    headers:{"Authorization":"Basic "+access_token},
    success:function(res){
      selectData = res.data;
      var systemList = selectData.system;
      for(var key in systemList){
        $('select[name="system"]').append('<option value="'+key+'">'+systemList[key]+'</option>');
      }
      form.render('select');
    }
  });
  
  //添加按钮点击事件
  form.on('submit(add)', function(data){
    data.field["userId"] = userId;  //表单数据中增加用户名
    $.ajax({
      url: 'actionApi/Type/add',
      type:'post',
      data:{
        "":JSON.stringify(data.field)
      },
      headers:{"Authorization":"Basic "+access_token},
      success:function(res){
        if(res.code==0){
          var id = layer.msg("添加成功", {
            time: false,   //取消自动关闭
            btn: ['完成', '继续添加'],
            btn1:function(){
              var view = sessionStorage.viewNow;
              $('#view').load('src/view/'+view+'.html');
              layer.close(id);
            },
            btn2:function(){
              $('input[name="name"]').val('');
              layer.close(id);
            }
          });
        }else{
          layer.msg("添加失败");
        }
      }
    });
    
    
    
    return false;
  });
  form.on('submit(cancel)', function(data){
    var view = sessionStorage.viewNow;
    $('#view').load('src/view/'+view+'.html');
  });
  
  
});




</script>